package org.dreamfly.core.service.impl;

import org.dreamfly.core.bean.SzAuthorities;
import org.dreamfly.core.bean.SzUser;
import org.dreamfly.core.bean.SzUsers;
import org.dreamfly.core.dao.AuthorityDAO;
import org.dreamfly.core.dao.UserDAO;
import org.dreamfly.core.service.UserService;

import java.util.List;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: gin
 * Date: 8/12/11
 * Time: 9:44 AM
 * To change this template use File | Settings | File Templates.
 */
public class UserServiceImpl implements UserService {
    private UserDAO userDAO;
    private AuthorityDAO authorityDAO;
    private String server;

    @Override
    public void addUser(SzUsers user) {
        userDAO.saveOrUpdate(user);
    }

    @Override
    public void updateUser(SzUsers user) {
        userDAO.saveOrUpdate(user);
    }

    @Override
    public void updateUserInfo(SzUsers user) {
        userDAO.merge(user);
    }

    @Override
    public void clearUserAuthorities(SzUsers user) {
        authorityDAO.deleteByUser(user.getId());
    }

    @Override
    public void deleteUser(SzUsers user) {
        userDAO.delete(user);
    }

    @Override
    public SzUsers findUser(Long id) {
        return userDAO.find(SzUsers.class, id);
    }

    @Override
    public SzUsers findUserByName(String username) {
        return userDAO.findByName(server,username);  //TODO
    }

    @Override
    public SzUsers findAdmin() {
        return userDAO.findAdmin(server);
    }

    @Override
    public List<SzUsers> findUserList() {
        return userDAO.findAllNative(SzUsers.class, server, false);
    }

    @Override
    public List<SzUsers> findNewUserList() {
        return userDAO.findAllNative(SzUsers.class, server, true);
    }

    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    public void setServer(String server) {
        this.server = server;
    }

    public void setAuthorityDAO(AuthorityDAO authorityDAO) {
        this.authorityDAO = authorityDAO;
    }
}
